import { debounce } from '@/utils/index.js'
import { emitter } from '@/utils/EventBus.js'

export default function (callback) {
  let resizeHandler = null

  onMounted(() => {
    initListener()
  })

  emitter.on('resetChart', () => {
    resize()
  })


  onBeforeUnmount(() => {
    window.removeEventListener('resize', resizeHandler)
    resizeHandler = null
  })

  function initListener() {
    resizeHandler = debounce(() => {
      resize()
    }, 100)
    window.addEventListener('resize', resizeHandler)
  }

  function resize() {
    callback && callback()
  }
}
